package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.CountryDTO;
import com.sky.dto.CountryPageQueryDTO;
import com.sky.entity.Country;
import com.sky.mapper.CountryMapper;
import com.sky.result.PageResult;
import com.sky.service.CountryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sky.entity.Country;
import java.util.List; 

import java.time.LocalDateTime;

@Service
public class CountryServiceImpl implements CountryService {
    @Autowired
    private CountryMapper countryMapper;

    /**
     * 根据领区id查询国家列表
     * @param regionId 领区id
     * @return 该领区下的国家列表
     */
    @Override
    public List<Country> getCountriesByRegionId(Long regionId) {
        return countryMapper.getByRegionId(regionId);
    }
    /**
     * 新增国家
     * @param countryDTO
     */
    @Override
    public void insert(CountryDTO countryDTO) {
        Country country = new Country();
        BeanUtils.copyProperties(countryDTO,country);
        country.setCreateTime(LocalDateTime.now());
        countryMapper.insert(country);

    }

    
    /**
     * 获取所有国家列表
     * @return 国家列表
     */
    @Override
    public List<Country> list() {
        return countryMapper.list();
    }

    /**
     * 启用或禁用国家
     * @param id
     * @param status
     */
    @Override
    public void startOrStop(Long id, Integer status) {
        Country country = Country.builder()
                        .id(id)
                        .status(status)
                        .build();
        countryMapper.update(country);

    }

    /**
     *  修改国家
     * @param countryDTO
     */
    @Override
    public void update(CountryDTO countryDTO) {
        Country country = new Country();
        BeanUtils.copyProperties(countryDTO,country);
        countryMapper.update(country);
    }

    /**
     * 删除国家
     * @param id
     */
    @Override
    public void delete(Long id) {
        countryMapper.delete(id);
    }

    /**
     * 分页查询国家
     * @param countryPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(CountryPageQueryDTO countryPageQueryDTO) {
        PageHelper.startPage(countryPageQueryDTO.getPage(),countryPageQueryDTO.getPageSize());
        Page<Country> page = countryMapper.page(countryPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 统计国家下的签证产品数量
     * @param id
     * @return
     */
    @Override
    public Integer countProductNumber(Long id) {
        return null;
    }
}
